package md.frolov.bible.providers;

import md.frolov.bible.model.IStructure;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class ViewContentProvider implements IStructuredContentProvider,ITreeContentProvider {
	private IStructure input;
	
	private Class topClass = Object.class;
	private Class bottomClass = Object.class;
	
	public ViewContentProvider(Class topClass, Class bottomClass) {
		this.topClass = topClass;
		this.bottomClass = bottomClass;
	}

	public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		input = (IStructure) newInput;
	}
	
	public void dispose() {
	}
	
	public Object[] getElements(Object parent) {
		if(input.getClass().equals(topClass))
			return getChildren(input);
		return new Object[]{};
	}
	
	public Object getParent(Object child) {
		if(child.getClass().equals(topClass)) return null;
		return ((IStructure) child).getParent();
	}
	
	public Object [] getChildren(Object parent) {
		if(parent.getClass().equals(bottomClass)) return new Object[0];
		return ((IStructure)parent).getChildren();
	}
	
	public boolean hasChildren(Object parent) {
		if(parent.getClass().equals(bottomClass)) return false;
		return ((IStructure)parent).hasChildren();
	}
}